package com.weme.mine.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import com.bumptech.glide.Glide
import com.weme.common.util.*
import com.weme.mine.R
import com.weme.mine.activity.UserInfoActivity
import com.weme.mine.databinding.ViewUserInfoYouBinding
import com.weme.mine.viewmode.UserInfoViewMode
import dev.kratos.api.user_server.v1.FollowRequest
import dev.kratos.api.user_server.v1.UnFollowRequest
import dev.kratos.api.user_server.v1.Users

class ViewUserInfoYou @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {
    private val mBinding = ViewUserInfoYouBinding.inflate(LayoutInflater.from(context), this, true)

    private var viewModel: UserInfoViewMode? = null


    init {

    }

    fun setInitView(activity: UserInfoActivity) {
        viewModel = findViewTreeViewModelStoreOwner()?.let {
            ViewModelProvider(activity).get(UserInfoViewMode::class.java)
        }
        mBinding.smvFollow.setOnClickListener {
            viewModel?.apply {
                if (isFollow) {
                    setUnFollowApi(UnFollowRequest.newBuilder().setUid(userId).build())
                } else {
                    setFollowApi(FollowRequest.newBuilder().setUid(userId).build())
                }
            }

        }
    }

    fun setMode(user: Users) {
        mBinding.root.fadeIn()
        if (!TextUtils.isEmpty(user.headPic)) {
            mBinding.imHead.loadImage(user.headPic)
        }
        when (user.onlineStatus) {
            1 -> {
                mBinding.vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
            }
            2 -> {
                mBinding.vCircle.setBackgroundResource(com.example.resources.R.drawable.busy_dot)
            }
            else -> {
                mBinding.vCircle.gone()
            }
        }

        mBinding.tvName.text = setNameAge(String.format(user.nickname + "," + user.age),6)
        mBinding.tvContent.text = user.intro

        // mBinding.tvLanguage.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, com.example.resources.R.mipmap.icon_global), null, null, null)
        mBinding.tvLanguage.text = user.lang
        mBinding.imFlag.setImageResource(com.example.resources.R.mipmap.icon_global)
        val country = getCountryBean(user.countryCode)
        country?.let {
            Glide.with(this).load(it.countryFlag).into(mBinding.imCountry)
        }
        if (user.isFollow == 1) {
            mBinding.smvFollow.setBackgroundResource(com.example.resources.R.drawable.bg_app_color_bk)
            mBinding.tvFollow.text = context.getString(com.example.resources.R.string.user_info_follow_yes)
            mBinding.tvFollow.setTextColor(ContextCompat.getColor(context, com.example.resources.R.color.app_btn_colors))
        } else {
            mBinding.smvFollow.setBackgroundResource(com.example.resources.R.drawable.bg_app_color_back)
            mBinding.tvFollow.text = context.getString(com.example.resources.R.string.user_info_follow)
            mBinding.tvFollow.setTextColor(ContextCompat.getColor(context, com.example.resources.R.color.white))
        }
    }


    fun setFollow() {
        if (viewModel!!.isFollow) {
            mBinding.smvFollow.setBackgroundResource(com.example.resources.R.drawable.bg_app_color_bk)
            mBinding.tvFollow.text = context.getString(com.example.resources.R.string.user_info_follow_yes)
            mBinding.tvFollow.setTextColor(ContextCompat.getColor(context, com.example.resources.R.color.app_btn_colors))
        } else {
            mBinding.smvFollow.setBackgroundResource(com.example.resources.R.drawable.bg_app_color_back)
            mBinding.tvFollow.text = context.getString(com.example.resources.R.string.user_info_follow)
            mBinding.tvFollow.setTextColor(ContextCompat.getColor(context, com.example.resources.R.color.white))
        }
    }
}